home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / lzw4c13.zip / TEST_LZW.C < prev    next >
Text File  |  1993-08-30  |  4KB  |  137 lines

  1. /*
  2. **   TEST_LZW.C       Copyright (C) 1992 by MarshallSoft Computing, Inc.
  3. **
  4. **   This program is used to compress, expand, and verify each specified
  5. **   file. It's purpose is for you to test the LZW4C library on your own
  6. **   files. Your files are never modified. However, you should NOT have a
  7. **   file named "XXX.XXX" or "YYY.YYY".  Compression ratios are printed
  8. **   for each file compressed. For example, to compress all files ending
  9. **   in *.C in your current directory, type:
  10. **
  11. **        TEST_LZW *.c
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <dos.h>
  16. #include <fcntl.h>
  17. #include <sys\types.h>
  18. #include <sys\stat.h>
  19. #include <io.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <conio.h>
  24.  
  25. #include "RW_IO.H"
  26. #include "LZW4C.H"
  27. #include "DIR_IO.H"
  28.  
  29. void SayError(int);
  30.  
  31. FILE *FilePtr1;
  32. FILE *FilePtr2;
  33.  
  34. void main(int argc,char *argv[])
  35. {int i, k;            /* loop counters */
  36.  int x, y;            /* last bytes read during compare */
  37.  int RetCode;         /* return code */
  38.  float Ratio;         /* compression ratio */
  39.  long Index;          /* byte count */
  40.  char Filename[15];   /* next filename */
  41.  int Files = 0;       /* # files compressed/expanded */
  42.  int BitCode;         /* 12, 13, or 14 */
  43.  /* begin */
  44.  switch(argc)
  45.    {case 2:
  46.       /* preferred value = 14 bits */
  47.       BitCode = 14;
  48.       break;
  49.     case 3:
  50.       /* get bit code value from command line */
  51.       BitCode = atoi(argv[2]);
  52.       break;
  53.     default:
  54.       printf("Usage: TEST_LZW <filespec> {<bitcode>}\n");
  55.       exit(1);
  56.    }
  57.  /* use 9 to <BitCode> bit codes */
  58.  RetCode = InitLZW(malloc,BitCode);
  59.  if(RetCode<0)
  60.    {SayError(RetCode);
  61.     exit(2);
  62.    }
  63.  /* flush the keyboard */
  64.  puts("\nTEST_LZW 1.1: Type any key to abort...");
  65.  for(i=0;;i++)
  66.    {if(kbhit())
  67.       {puts("\n...Aborted by user !");
  68.        break;
  69.       }
  70.     /* get next filename */
  71.     if(i==0) RetCode = FindFirst(argv[1],Filename);
  72.     else RetCode = FindNext(Filename);
  73.     if(!RetCode) break;
  74.     /* force to uppercase */
  75.     for(k=0;k<strlen(Filename);k++) Filename[i] = toupper(Filename[i]);
  76.     /* skip 'work' files XXX.XXX and YYY.YYY */
  77.     if( (strcmp(Filename,"XXX.XXX")==0) ||
  78.         (strcmp(Filename,"YYY.YYY")==0) ) continue;
  79.     /* open input file for compression */
  80.     if(!ReaderOpen(Filename)) exit(3);
  81.     /* open output file for compression */
  82.     if(!WriterOpen("XXX.XXX")) exit(4);
  83.     /* do the compression */
  84.     Files++;
  85.     printf("\nCompressing %12s ",Filename);
  86.     if((RetCode=Compress(Reader,Writer))<0)
  87.       {SayError(RetCode);
  88.        exit(5);
  89.       }
  90.     /* report compression ratio */
  91.     if(ReaderCount() > 0)
  92.        {Ratio = (float)(WriterCount())/(float)ReaderCount();
  93.         printf(" OK (%0.2f)\n",Ratio);
  94.        }
  95.     else puts("???");
  96.     /* close files */
  97.     ReaderClose();
  98.     WriterClose();
  99.     /* open input file for expansion */
  100.     if(!ReaderOpen("XXX.XXX")) exit(6);
  101.     /* open output file for expansion */
  102.     if(!WriterOpen("YYY.YYY")) exit(7);
  103.     /* do the expansion */
  104.     printf("  Expanding %12s ",Filename);
  105.     if((RetCode=Expand(Reader,Writer))<0)
  106.       {printf("Expand returns error %d\n",RetCode);
  107.       }
  108.     /* close files */
  109.     ReaderClose();
  110.     WriterClose();
  111.     printf(" OK\n");
  112.     /* compare original to expanded file */
  113.     FilePtr1 = fopen(Filename,"rb");
  114.     FilePtr2 = fopen("YYY.YYY","rb");
  115.     printf("  Comparing              ");
  116.     Index = 0;
  117.     while(1)
  118.       {x = fgetc(FilePtr1);
  119.        y = fgetc(FilePtr2);
  120.        Index++;
  121.        /* print dot every 4K bytes */
  122.        if((Index&0x0fff)==0) putchar('.');
  123.        if((x==EOF)&&(y==EOF)) break;
  124.        if(x!=y)
  125.          {printf("ERROR: Difference between files at index %ld\n",Index-1);
  126.           exit(8);
  127.          }
  128.       }
  129.     printf(" OK\n");
  130.     fclose(FilePtr1);
  131.     fclose(FilePtr2);
  132.    }
  133.  /* all done */
  134.  TermLZW(free);
  135.  printf("\n%d files tested\n",Files);
  136.  exit(0);
  137. }